package com.mes.kpi.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessResourceFailureException;

import com.mes.common.dao.BasicDao;
import com.mes.kpi.dao.iface.IKPIHeaderDao;
import com.mes.kpi.domain.KPIHeader;

public class KPIHeaderDao extends BasicDao<KPIHeader> implements IKPIHeaderDao {

	@Override
	public List<KPIHeader> getDeptKPI(String deptId, String kpiDefinitonId) {
		// TODO Auto-generated method stub
		List list=null;
			try {
				Session session=this.getSession();
				StringBuilder hql=new StringBuilder();
				hql.append("from "+ KPIHeader.class.getName() +" where ");
				hql.append( " deptUid=? ");
				hql.append( "and  kpiDefinitonId=? order by kpiDefinitonId");
				Query query = session.createQuery(hql.toString());

				query.setString(0, deptId);
				query.setString(1, kpiDefinitonId);
				list = query.list();
			} catch (DataAccessResourceFailureException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return list;
		}
		
		
	@Override
	public List<KPIHeader> getChildDeptKPI(String deptUid, String kpiDefinitonId) {
		// TODO Auto-generated method stub
		List list=null;
			try {
				Session session=this.getSession();
				StringBuilder hql=new StringBuilder();
				hql.append("from "+ KPIHeader.class.getName() +" where ");
				hql.append( " dept.parentDeptUid = ?");
				hql.append( "and  kpiDefinitonId=? order by kpiDefinitonId");
				Query query = session.createQuery(hql.toString());

				query.setString(0, deptUid);
				query.setString(1, kpiDefinitonId);
				list = query.list();
			} catch (DataAccessResourceFailureException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return list;
		}

	
}
